package DoExercise.HSP_ZCY.A009_图结构.A001_并查集;

import java.util.HashSet;
import java.util.Stack;

/**
 *
 * 图的深度遍历
 *
 */
public class Code02_深度优先遍历
{
    public static void DFS(Node node)
    {
        if (node == null) return;
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        stack.add(node);
        set.add(node);
        System.out.println(node.value);
        while (!stack.isEmpty())
        {
            Node cur = stack.pop();
            for (Node next : cur.nexts)
            {
                if (!set.contains(next))
                {
                    stack.push(cur);//回溯用
                    stack.push(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
        }
    }
    
}
